package com.treetao.union.core.amap.geocode.geo;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.treetao.union.core.util.HttpKit;

import lombok.extern.slf4j.Slf4j;

/**
 * 高德地图-地理编码
 * 
 * @Author devm
 * @date 2020年6月7日
 * @see https://gitee.com/devm/devm-mall-server
 */
@Slf4j
public final class GeocodeApi {
	private static final String url = "https://restapi.amap.com/v3/geocode/geo?key=%s&address=%s&output=JSON";
	/**
	 * 地理编码
	 * @param key 应用KEY @see https://lbs.amap.com/dev/key
	 * @param address 具体地址
	 * @return 只有正确时才返回结果，否则返回 null
	 * @see https://lbs.amap.com/api/webservice/guide/api/georegeo
	 */
	public static GeocodeResp getGeocode(String key,String address) {
		if(StringUtils.isEmpty(key)) {
			log.error("amap [key] required !");
			return null;
		}
		if(StringUtils.isEmpty(key)) {
			log.error("amap geocode [address] required !");
			return null;
		}
		String res = HttpKit.get(String.format(url, key,address));
		GeocodeResp resp = new Gson().fromJson(res, GeocodeResp.class);
		if(!"1".equals(resp.getStatus())) {
			log.error(resp.getInfo());
			return null;
		}
		return resp;
	}
}
